/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.TX.CustomerIOTRPortal;


import session.TX.*;
import entity.*;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Chen Zhe
 */
@ManagedBean (name="fakeMainPortalMB")
@SessionScoped
public class FakeMainPortalMB implements Serializable{

    /** Creates a new instance of FakeMainPortalMB */
    public FakeMainPortalMB() { 
    }
    
    @EJB
    private ShoppingCartSBRemote cartManager;
    public ShoppingCartSBRemote getCartManager() {return cartManager;}
    
    @EJB
    private UsersSBRemote userManager;
    
    @EJB
    private SchedulerSBRemote schedulerBean;
    
    @EJB
    private OutletSBRemote outletManager;
    
    @EJB
    private StakeholderAccountsSBRemote stakeholderManager;
    
    @EJB
    private ULPSBRemote ULPManager;
    public ULPSBRemote getULPManager() {return ULPManager;}
    
    private Long customerID;
    private String customerEmail = "N/A";
    private String outletName;
    private String stakeholderName;
    private Long outletId;
    private Long selectedReward_id;
    
    
    
    
    //Randall 12-OCT-2011 1531hrs
    /*Login method used temporarily for testing purpose, code derived from SM team
    * The way they do it is to push an attribute into the session, the customer ID (a Long type)
    * with the attribute name "Sid", this keeps track of which customer is currently logged in.
    */
    public void login(){
        System.out.println(">>> fakeMainPortalMB: Login sequence initiated");
        //TODO code to obtain the customer ID, given the customer email
        customerID = new Long(0); //take the default to be 0 for the moment.
        
        List<Users> userList = userManager.getAllUser();
        
        for(Users o: userList){
            if(o.getEmail().equalsIgnoreCase(customerEmail)){
                System.out.println(">>> fakeMainPortalMB: user account found: " + o.getEmail());
                customerID = o.getId();
                break;
            }
        } 
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
        session.setAttribute("Sid", customerID);

        
        System.out.println(">>> fakeMainPortalMB: customer logged in: " + session.getAttribute("Sid"));
        System.out.println(">>> Session ID: " + session.getId());
    }
    
    //Randall 12-OCT-2011 1650hrs
    //Used for the Main page, show all the users in the DB, choose one to login
    public List<String> getAllUserEmails(){
        List<String> returnList = new ArrayList<String>();   
        List<Users> userList = userManager.getAllUser(); 
        if (userList == null || userList.isEmpty())
            return null;
        for(Users o: userList){
            returnList.add(o.getEmail());
        }
        return returnList;
    }
    
    
    public List<Outlet> getOutletsbyStakeholder(){
        List<Outlet> returnList = outletManager.getOutletByStakeholder(stakeholderName);      
        return returnList;
    }
    
    
    public List<String> getAllFoodStakeholderNames(){       
        List<String> returnList = new ArrayList<String>();
        
        for(Stakeholder o : stakeholderManager.getAllStakeholders()){
            if (o.getClass() == StakeholderFood.class)
            returnList.add(o.getName());
        }
        
        return returnList;
    }
    

    
    
    
    
    //=============================================================================================================================================
    //Randall Added: Code for ULP functions
    //=============================================================================================================================================
    public List<RewardItem> getRewardsbyStakeholder(){
        return ULPManager.getRewardsByStakeholder(stakeholderName);
    }
    
    
    public Integer getUserLoyaltyPoints(){
        List<Users> userList = userManager.getAllUser();
        if (userList == null)
            return null;
        
        for(Users o: userList){
            if(o.getEmail().equalsIgnoreCase(customerEmail)){
                System.out.println(">>> fakeMainPortalMB: user account found: " + o.getEmail() + "    Loyalty Points: " +  o.getLoyaltypoint() );
                return o.getLoyaltypoint();
            }
        }
        return 0;
    }
    
    
    public void buyRewardVoucher(long id){
        System.out.println(">>> CUSTOMER BUY VOUCHER");
        buyVoucherMSG = ULPManager.buyVoucher(customerEmail, id);
        selectedReward_id = new Long(0);
    }
    
    private String buyVoucherMSG;
    public String getBuyVoucherMSG() {return buyVoucherMSG;}
    public void setBuyVoucherMSG(String buyVoucherMSG) {this.buyVoucherMSG = buyVoucherMSG;}
    
    
    
    public Integer retrieveStakeholderLoyaltyPoints(){
        List<Stakeholder> stkList = stakeholderManager.getSpecificStakeholders(stakeholderName);
        if(!stkList.isEmpty()){
            return stkList.get(0).getLoyaltyPoints();
        }
        return 0;
    }
    
    //=============================================================================================================================================


    /**
     * @return the customerEmail
     */
    public String getCustomerEmail() {
        return customerEmail;
    }

    /**
     * @param customerEmail the customerEmail to set
     */
    public void setCustomerEmail(String customerEmail) {
        this.customerEmail = customerEmail;
    }

    /**
     * @return the outletName
     */
    public String getOutletName() {
        return outletName;
    }

    /**
     * @param outletName the outletName to set
     */
    public void setOutletName(String outletName) {
        this.outletName = outletName;
    }

    public Long getOutletId() {
        return outletId;
    }

    public void setOutletId(Long outletId) {
        this.outletId = outletId;
    }
    
    public void startBilling(){
        schedulerBean.enableBilling();
    }
    public void stopBilling(){
        schedulerBean.disableBilling();
    }

    public String getStakeholderName() {
        return stakeholderName;
    }

    public void setStakeholderName(String stakeholderName) {
        this.stakeholderName = stakeholderName;
    }
    
    /**
     * @return the customerID
     */
    public Long getCustomerID() {
        return customerID;
    }

    /**
     * @param customerID the customerID to set
     */
    public void setCustomerID(Long customerID) {
        this.customerID = customerID;
    }

    /**
     * @param cartManager the cartManager to set
     */
    public void setCartManager(ShoppingCartSBRemote cartManager) {
        this.cartManager = cartManager;
    }

    /**
     * @return the selectedReward_id
     */
    public Long getSelectedReward_id() {
        return selectedReward_id;
    }

    /**
     * @param selectedReward_id the selectedReward_id to set
     */
    public void setSelectedReward_id(Long selectedReward_id) {
        this.selectedReward_id = selectedReward_id;
    }
    
    
    
}